package com.xyebank.ledger.service.zy.adapter;

import org.apache.commons.lang3.StringUtils;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.stereotype.Component;

import com.xyebank.ledger.db.entitys.LPaymentInf;
import com.xyebank.ledger.service.common.conveter.PayBeanConverter;
import com.xyebank.ledger.service.constant.CommonConstant;
import com.xyebank.ledger.service.constant.ProductConstant;
import com.xyebank.ledger.util.IDUtil;
import com.xyebank.ledger.util.StatusConvertUtil;
import com.xyebank.ledger.vo.deal.ZYCashPaymentInfVo;

@Component(value = "zyPayBeanConverter")
public class ZYPayBeanConverter implements PayBeanConverter<ZYCashPaymentInfVo> {

    ModelMapper modelMapper = new ModelMapper();

    class ZYPayPropertyMap extends PropertyMap<ZYCashPaymentInfVo, LPaymentInf> {
        protected void configure() { 
            map(source.getMblNo(), destination.getMblNo());
            map(source.getOrderNo(), destination.getOrgOrderNo());
            map(source.getLoanAmount(), destination.getLoanAmount());
            map(source.getLoanTime(), destination.getLoanTime());
            map(source.getLoanPeriod(), destination.getLoanPeriod());

        }
    }

    public ZYPayBeanConverter() {
        modelMapper.addMappings(new ZYPayPropertyMap());
    }

    @Override
    public LPaymentInf covert(ZYCashPaymentInfVo x) {
        LPaymentInf entity = modelMapper.map(x, LPaymentInf.class);
        entity.setId(IDUtil.generateId(x.getId().toString(), "zy"));
        buildDataSource(x, entity);

        entity.setOrgNo(ProductConstant.ZY_LOAN_ORG);
        entity.setProductNo(ProductConstant.ZY_LOAN_PROD_ID);
        if(x.getLoanState() != null)entity.setApprovalStatus(StatusConvertUtil.convertApplyStatus(x.getLoanState()));
        if(x.getLoanState() != null)entity.setLoanStatus(StatusConvertUtil.convertPayStatus(x.getLoanState()));
        return entity;
    }
    
    
    private void buildDataSource(ZYCashPaymentInfVo x, LPaymentInf entity) {
        String loanNo = x.getLoanNo();
        String str = loanNo.substring(5, 6);
        if(StringUtils.equals(str, "4")){
            entity.setDataSource(CommonConstant.SJD_STR);
            
        } else if(StringUtils.equals(str, "3")){
            entity.setDataSource(CommonConstant.JRY_STR);
            
        } else{
            entity.setDataSource(CommonConstant.UNKOWN_STR);
            
        }
    }
}
